package com.saturn.titan.web.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.saturn.titan.core.bean.BusiRepairBill;
import com.saturn.titan.core.bean.BusiWorkBill;
import com.saturn.titan.core.dto.RepairBillDto;
import com.saturn.titan.core.service.RepairAndInstallBillService;

/**
 * @author liuwenhao
 */
@Controller
@RequestMapping(value = "/ri")
public class RepareAndInstallController {
	
	@Autowired
	private RepairAndInstallBillService repairAndInstallBillService;
	
	@RequestMapping(value = "selectRepare", method = RequestMethod.GET)
	public String selectRepare() throws ParseException {
		
		return "repareBill/selectRepare";
	}
	
	@RequestMapping(value = "selectInstall", method = RequestMethod.GET)
	public String selectInstall() throws ParseException {
		return "installBIll/selectInstallBill";
	}
	
	@RequestMapping(value = "viewRepare", method = RequestMethod.POST)
	public String viewRepare(@RequestParam(value="id") Long id,Model model) throws ParseException {
		BusiRepairBill rb = repairAndInstallBillService.findRepairBillById(id);
		model.addAttribute("repairBill", rb);
		return "repareBill/viewRepare";
	}
	
	@RequestMapping(value = "viewInstall", method = RequestMethod.POST)
	public String viewInstall(@RequestParam(value="id") Long id,Model model) throws ParseException {
		//TODO
		return "installBIll/viewInstall";
	}
	
	@RequestMapping(value = "selRepairByCondition")
	@ResponseBody
	public  Map<String, Object> selectRepairBill(
			@RequestParam(value="billNo") String billNo ,
			@RequestParam(value="workBillId") Integer workBillId,
			@RequestParam(value="deployBillId") Integer deployBillId,
			@RequestParam(value="billStt") String billStt,
			@RequestParam(value="emergentType") String emergentType,
			@RequestParam(value="empName") String empName,
			@RequestParam(value="startDate_begin") String startDate_begin,
			@RequestParam(value="startDate_end") String startDate_end,
			@RequestParam(value="endDate_begin") String endDate_begin,
			@RequestParam(value="endDate_end") String endDate_end,
			@RequestParam(value="page") Integer page,
			@RequestParam(value="rows") Integer rows
			) throws ParseException {
		
		List<RepairBillDto> list = repairAndInstallBillService.getBusiRepairBillByCondition(billNo,workBillId,deployBillId,billStt,emergentType,empName,startDate_begin,startDate_end,endDate_begin,endDate_end);
		
		List<RepairBillDto> returnList = new ArrayList<RepairBillDto>();
		
		int size = list.size();
		int beginIndex = (page-1) * rows;
		int endIndex = page*rows-1;
		
		if(endIndex > size-1){
			endIndex = size-1;
		}
		for(int i=beginIndex;i<=endIndex;i++){
			returnList.add(list.get(i));
		}
		
		Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map  
		   jsonMap.put("total",list.size());//total键 存放总记录数，必须的  
	        jsonMap.put("rows", returnList);//rows键 存放每页记录 list  
		return jsonMap;
	}
}
